package com.tiancheng.trade.merchant.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.model.Business;
import com.tiancheng.trade.merchant.model.Merchant;
import com.tiancheng.trade.merchant.model.OrgBusiness;
import com.tiancheng.trade.merchant.model.Organization;
import com.tiancheng.trade.merchant.service.IOrgBusinessService;
import com.tiancheng.trade.merchant.service.IOrganizationService;
import com.tiancheng.trade.merchant.util.ResultJudgeUtil;
import com.tiancheng.trade.merchant.vo.merchant.*;
import com.tiancheng.trade.merchant.vo.organization.CreateOrganizationVO;
import com.tiancheng.trade.merchant.vo.organization.UpdateOrganizationVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/admin/orgBusiness")
public class OrgBusinessAdminController {
    @Autowired
    private IOrgBusinessService orgBusinessService;

    /**
     * 按id查询机构业务信息
     *
     * @param id {@link Long}
     * @return {@link Result}<{@link Merchant}>
     */
    @GetMapping("/{id}")
    public Result<OrgBusiness> findById(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(orgBusinessService.getById(id));
    }

    /**
     * 分页查询机构业务信息
     *
     * @param conditions {@link GetPageMerchantVO}
     * @return {@link Result}<{@link PageInfo}<{@link Merchant}>>
     */
    @GetMapping("/queryPage")
    public Result<PageInfo<QueryOrgBusinessResVO>> findMerchantPage(QueryOrgBusinessReqVO conditions) throws BusinessException {
        return Result.success(orgBusinessService.queryPage(conditions));
    }

    /**
     * 查询机构业务信息列表
     *
     * @param conditions {@link GetMerchantVO}
     * @return {@link Result}<{@link List}<{@link Merchant}>>
     */
    @GetMapping("/queryList")
    public Result<List<QueryOrgBusinessResVO>> queryList(QueryOrgBusinessReqVO conditions) throws BusinessException {
        return Result.success(orgBusinessService.queryList(conditions));
    }

    /**
     * 新增业务信息
     *
     * @param business {@link Merchant}
     */
    @PostMapping("/")
    public Result<?> add(@RequestBody @Valid OrgBusiness business) throws BusinessException {
        orgBusinessService.saveOrgBusiness(business);
        return Result.success();
    }

    /**
     * 修改商户信息
     *
     * @param business {@link Merchant}
     */
    @PutMapping("/")
    public Result<?> update(@RequestBody OrgBusiness business) throws BusinessException {
        Assert.isTrue(business.getId() != null, "参数错误~");
        orgBusinessService.updateById(business);
        return Result.success();
    }

    /**
     * 按商户id删除商户信息，同时删除商户渠道信息
     *
     * @param id {@link Long}
     */
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        orgBusinessService.delete(id);
        return Result.success();
    }

}
